package com.luanye.enjoy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luanye.enjoy.constants.CommunityConst;
import com.luanye.enjoy.entity.Activity;
import com.luanye.enjoy.entity.Tribe;
import com.luanye.enjoy.entity.User;
import com.luanye.enjoy.entity.UserActivity;
import com.luanye.enjoy.mapper.ActivityMapper;
import com.luanye.enjoy.mapper.UserActivityMapper;
import com.luanye.enjoy.mapper.UserMapper;
import com.luanye.enjoy.service.ActivityService;
import com.luanye.enjoy.service.TribeService;
import com.luanye.enjoy.service.UserService;
import com.luanye.enjoy.utils.BeanCopyUtils;
import com.luanye.enjoy.vo.*;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service("activityService")
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private TribeService tribeService;

    @Autowired
    private UserActivityMapper userActivityMapper;

    @Autowired
    private UserMapper userMapper;



    @Override
    public RespBean getIndex(String openid) {
        //平台专题推荐
        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Activity::getTypeId, CommunityConst.ACTIVITY_TYPE_SYS);
        queryWrapper.orderByDesc(Activity::getJoinNum);
        List<Activity> activities = activityMapper.selectList(queryWrapper);
        Activity sysAct = null;
        if (activities.size() > 0){
            sysAct = activities.get(0);
            judgeStatus(sysAct);
        }
        ActivityVo activityVoSys = null;
        if (sysAct != null)
            activityVoSys = BeanCopyUtils.copyBean(sysAct, ActivityVo.class);
        //附近专题推荐
        LambdaQueryWrapper<Activity> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Activity::getTypeId, CommunityConst.ACTIVITY_TYPE_NEAR);
        queryWrapper1.orderByDesc(Activity::getJoinNum);

        List<Activity> activities1 = activityMapper.selectList(queryWrapper1);
        Activity nearAct = null;
        if (activities1.size() > 0)
            nearAct = activities1.get(0);
        ActivityVo activityVoNear = null;
        if (Objects.nonNull(nearAct)){
            judgeStatus(nearAct);
            activityVoNear = BeanCopyUtils.copyBean(nearAct, ActivityVo.class);
        }


        //附近活动
        User userByOpenId = userMapper.getUserByOpenId(openid);
        LambdaQueryWrapper<Activity> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.like(Activity::getLocation,userByOpenId.getCity());
        queryWrapper2.orderByAsc(Activity::getJoinNum);
        Page<Activity> page = new Page<>(1,6);
        page(page,queryWrapper2);
        List<Activity> records = page.getRecords();
        for (Activity activity : records){
            if (Objects.nonNull(activity))
                judgeStatus(activity);
        }
        //部落推荐
        RespBean allByNum = tribeService.getAllByNum();


        List<ActivityVo> activityVosAllNears = BeanCopyUtils.copyBeanList(records, ActivityVo.class);


        IndexVo indexVo = new IndexVo();
        indexVo.setSysAct(activityVoSys);
        indexVo.setNearAct(activityVoNear);
        indexVo.setNearActs(activityVosAllNears);
        if (Objects.nonNull(allByNum.getObj()))
            indexVo.setTribes((List<TribeVo>) allByNum.getObj());
        return RespBean.success(indexVo);
    }

    private void judgeStatus(Activity activity){

        Date date = new Date();
        if (date.before(activity.getBeginTime()))
            activity.setStatus(2);
        else if (date.before(activity.getEndTime()) && date.after(activity.getBeginTime()))
            activity.setStatus(1);
        else
            activity.setStatus(0);

    }


    @Override
    public RespBean getDetail(Long id, String openid) {
        Activity byId = getById(id);
        judgeStatus(byId);

        ActivityDetailVo activityDetailVo = BeanCopyUtils.copyBean(byId, ActivityDetailVo.class);
        activityDetailVo.setIsTakePartIn(false);
        Tribe tribe = tribeService.getById(byId.getTrideId());
        activityDetailVo.setTridename(tribe.getTrideName());

        activityDetailVo.setTribeLoc(tribe.getLocation());

        activityDetailVo.setIsCreator(byId.getCreateId().equals(openid));

        User userByOpenId = userMapper.getUserByOpenId(byId.getCreateId());
        if (byId.getCreateId().equals(openid))
            activityDetailVo.setIsTakePartIn(true);
        UserVo userVo = BeanCopyUtils.copyBean(userByOpenId, UserVo.class);
        activityDetailVo.setCreateUser(userVo);

        LambdaQueryWrapper<UserActivity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserActivity::getActivityId,id);
        List<UserActivity> userActivities = userActivityMapper.selectList(queryWrapper);
        List<UserVo> members = new ArrayList<>();
        for (UserActivity activity : userActivities){
            User userByOpenId1 = userMapper.getUserByOpenId(activity.getOpenId());
            if (userByOpenId1.getOpenId().equals(openid))
                activityDetailVo.setIsTakePartIn(true);
            UserVo userVo1 = BeanCopyUtils.copyBean(userByOpenId1, UserVo.class);
            members.add(userVo1);
        }
        activityDetailVo.setMembers(members);
        //当前用户是否参与

        return RespBean.success(activityDetailVo);
    }

    @Override
    public RespBean create(Activity activity, String openid) {
        activity.setCreateId(openid);
        save(activity);
        return RespBean.success();
    }

    @Override
    public RespBean delete(Long id, String openid) {
        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Activity::getId,id);
        queryWrapper.eq(Activity::getCreateId,openid);
        activityMapper.delete(queryWrapper);
        return RespBean.success();
    }

    @Override
    public RespBean search(String keyword) {
        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Activity::getTitle,keyword);

        List<Activity> activities = activityMapper.selectList(queryWrapper);
        List<ActivityVo> activityVos = new ArrayList<>();
        for (Activity activity : activities){
            judgeStatus(activity);
            String trideName = tribeService.getById(activity.getTrideId()).getTrideName();
            ActivityVo activityVo = BeanCopyUtils.copyBean(activity, ActivityVo.class);
            activityVo.setTrideName(trideName);
            activityVos.add(activityVo);
        }

        return RespBean.success(activityVos);
    }


    @Override
    public RespBean join(Long id, String openid) {
        Activity byId = getById(id);
        byId.setJoinNum(byId.getJoinNum() + 1);
        updateById(byId);

        UserActivity userActivity = new UserActivity();
        userActivity.setActivityId(id);
        userActivity.setOpenId(openid);
        userActivityMapper.insert(userActivity);
        return RespBean.success();
    }

    @Override
    public RespBean exit(Long id, String openid) {
        Activity activity = getById(id);
        activity.setJoinNum(activity.getJoinNum() - 1);
        updateById(activity);

        LambdaQueryWrapper<UserActivity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserActivity::getActivityId,id);
        queryWrapper.eq(UserActivity::getOpenId,openid);
        userActivityMapper.delete(queryWrapper);
        return RespBean.success();
    }

    @Override
    public List<ActivityVo> getByTribeId(Integer tribeId) {
        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Activity::getTrideId,tribeId);

        List<Activity> activities = activityMapper.selectList(queryWrapper);

        List<ActivityVo> activityVos = new ArrayList<>();

        for (Activity activity : activities){
            ActivityVo activityVo = BeanCopyUtils.copyBean(activity, ActivityVo.class);
            activityVo.setTrideName(tribeService.getById(activity.getTrideId()).getTrideName());
            activityVos.add(activityVo);
        }

        return activityVos;
    }
}
